package com.itany.boot.nmms.service.impl;

import com.itany.boot.nmms.constant.StatusConstant;
import com.itany.boot.nmms.dao.ProductTypeMapper;
import com.itany.boot.nmms.entity.ProductType;
import com.itany.boot.nmms.entity.ProductTypeExample;
import com.itany.boot.nmms.exception.ProductTypeExistException;
import com.itany.boot.nmms.exception.RequestParamsException;
import com.itany.boot.nmms.service.ProductTypeService;
import com.itany.boot.nmms.util.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProductTypeServiceImpl implements ProductTypeService {
    @Autowired
    private ProductTypeMapper typeMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<ProductType> findAll() {
        ProductTypeExample example = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = example.createCriteria();
        List<ProductType> productTypes = typeMapper.selectByExample(example);
        return productTypes.isEmpty()? null:productTypes;
    }

    @Override
    public void add(String name) throws RequestParamsException, ProductTypeExistException {
        if (ParamsUtil.isNull(name)) {
            throw new RequestParamsException("请求参数异常");
        }

        ProductTypeExample example = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        List<ProductType> productTypes = typeMapper.selectByExample(example);
        if (!productTypes.isEmpty()) {
            throw new ProductTypeExistException("该商品类型已经存在");
        }

        ProductType productType = new ProductType();
        productType.setName(name);
        productType.setStatus(Integer.valueOf(StatusConstant.ENABLE));
        typeMapper.insertSelective(productType);

    }

    @Override
    public void modifyStatus(String id) throws RequestParamsException {
        if (ParamsUtil.isNull(id)) {
            throw new RequestParamsException("请求参数异常");
        }

        ProductType productType = typeMapper.selectByPrimaryKey(Integer.valueOf(id));
        ProductType type = new ProductType();
        type.setId(Integer.valueOf(id));
        if (productType.getStatus().equals(StatusConstant.STAFF_IS_VALID_ENABLE)) {
            type.setStatus(StatusConstant.STAFF_IS_VALID_DISABLE);

        } else if (productType.getStatus().equals(StatusConstant.STAFF_IS_VALID_DISABLE)) {
            type.setStatus(StatusConstant.STAFF_IS_VALID_ENABLE);
        }
        typeMapper.updateByPrimaryKeySelective(type);
    }

    @Override
    public ProductType findById(String id) {
        ProductType productType = typeMapper.selectByPrimaryKey(Integer.valueOf(id));
        return productType;
    }

    @Override
    public void modify(String id, String name) throws RequestParamsException, ProductTypeExistException {
        if (ParamsUtil.isNull(id) || ParamsUtil.isNull(name)) {
            throw new RequestParamsException("请求参数异常");
        }
        ProductTypeExample example = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name)
                .andIdNotEqualTo(Integer.valueOf(id));
        List<ProductType> productTypes = typeMapper.selectByExample(example);
        if (!productTypes.isEmpty()) {
            throw new ProductTypeExistException("该商品类型已经存在");
        }

        ProductType productType = new ProductType();
        productType.setId(Integer.valueOf(id));
        productType.setName(name);
        typeMapper.updateByPrimaryKeySelective(productType);

    }

    @Override
    public List<ProductType> findAllByStatus() {
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andStatusEqualTo(Integer.valueOf(StatusConstant.ENABLE));
        List<ProductType> productTypes = typeMapper.selectByExample(example);
        return productTypes;
    }
}
